package com.zhenyi.youqu.admin.index;

import java.sql.SQLException;
import java.util.List;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.youqu.common.model.YqApp;
import com.youqu.common.model.YqShopUser;

public class ShopUserController extends YQAdminController {

	public void list() {
		YqShopUser user = getShopUser();

		Kv kv = getSimpleKv("keyword", "shopId");
		if (!YqShopUser.ROLE_SUPER_ADMIN.equals(user.getRole())) {
			kv.set("appId", user.getAppId());
		}
		if (user.getRole().equals(YqShopUser.ROLE_SHOP)) {
			kv.set("shopId", user.getShopId());
		}
		SqlPara sp = db.getSqlPara("YQAdminShopUsers", kv);
		Page<YqShopUser> pages = YqShopUser.dao.paginate(getPage(), getPageSize(), sp);
		setRainbow(pages);
		setItems(pages.getList());
		render("/yq/admin/shopUser/list.html");
	}

	public void info() {
		setReferer();
		YqShopUser loginUser = getShopUser();
		Long id = getParaToLong("id");
		YqShopUser user;
		if (id != null) {
			user = YqShopUser.dao.findById(id);
			if (user == null) {
				renderError(404);
				return;
			}
		} else {
			user = new YqShopUser();
			user.setRole(YqShopUser.ROLE_SHOP);
			user.setStatus(1);
			if (loginUser.getRole().equals(YqShopUser.ROLE_SHOP)) {
				user.setShopId(loginUser.getShopId());
			}
		}

		SqlPara sp = db.getSqlPara("YQShops", Kv.by("appId", loginUser.getAppId()));
		List<Record> shops = db.find(sp);
		setAttr("shops", JsonKit.toJson(shops));
		setAttr("data", JsonKit.toJson(user));
		List<YqApp> apps = YqApp.dao.findAll();
		setData("apps", apps);
		render("/yq/admin/shopUser/info.html");
	}

	public void submit() {

		Long id = getParaToLong("id");
		String name = getPara("name");
		String account = getPara("account");
		String password = getPara("password");
		if (StrKit.isBlank(account) || StrKit.isBlank(password)) {
			renderMsg("账号密码不能为空");
			return;
		}
		Integer role = getParaToInt("role", YqShopUser.ROLE_SHOP);
		Long shopId = getParaToLong("shopId");
		if (shopId == null) {
			renderMsg("请选择商户");
			return;
		}
		String mobile = getPara("mobile");
		Integer status = getParaToInt("status");
		Long appId = getParaToLong("appId");

		boolean flag = Db.tx(new IAtom() {

			@Override
			public boolean run() throws SQLException {
				YqShopUser user = getShopUser();
				YqShopUser info = null;
				if (id == null) {
					try {
						info = new YqShopUser();
						info.setAccount(account);
						info.save();
					} catch (Exception e) {
						renderMsg("账号已存在");
						return false;
					}
				} else {
					info = YqShopUser.dao.findById(id);
					if (info == null) {
						renderError(404);
						return false;
					}
				}
				info.setAppId(appId == null ? user.getAppId() : appId);
				info.setName(name);
				info.setAccount(account);
				info.setPassword(password);
				info.setRole(role);
				info.setShopId(shopId);
				info.setMobile(mobile);
				info.setStatus(status);
				boolean flag = info.update();
				if (!flag) {
					renderMsg("保存失败");
				}
				return flag;
			}
		});
		if (flag) {
			renderSuccess();
		}
	}

	public void delete() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderError(500);
			return;
		}
		YqShopUser info = YqShopUser.dao.findById(id);
		if (info == null) {
			renderError(404);
			return;
		}
		renderFlag(info.delete());
	}

}
